package com.xsedge.xsedge.controller;/*
 * @Author ljc
 * @Description //TODO $
 * @Param $
 * @return $
 **/

import com.xsedge.xsedge.dao.ContainersRepository;
import com.xsedge.xsedge.dao.ImageRepository;
import com.xsedge.xsedge.dao.NodeRepository;
import com.xsedge.xsedge.entity.Container;
import com.xsedge.xsedge.entity.Images;
import com.xsedge.xsedge.entity.NodeInfo;
import com.xsedge.xsedge.entity.PodInfo;
import com.xsedge.xsedge.service.KillDockerService;
import com.xsedge.xsedge.service.RunDockerApiService;
import com.xsedge.xsedge.service.RunDockerService;
import com.xsedge.xsedge.tools.Result;
import com.xsedge.xsedge.tools.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

@RestController
public class ContainersController {

	private static final Logger logger = Logger.getLogger(String.valueOf(ContainersController.class));

	@Autowired
	ContainersRepository containersRepository;

	@Autowired
	NodeRepository nodeRepository;

	@Autowired
	ImageRepository imageRepository;

	@Autowired
	private RunDockerService rundockerService;

	@Autowired
	private RunDockerApiService runDockerApiService;
	@Autowired
	private KillDockerService killDockerService;

	@RequestMapping(value = "/api/start", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
	@ResponseBody
	public Result startContainer(@NotNull @RequestParam("node_ip")  String node_ip,@NotNull @RequestParam("image_name")  String image_name) throws IOException {

		Map<String,Object> map =runDockerApiService.runDocker(node_ip,image_name);
		//解析PodInfo，and docker run
		logger.info("node_ip:"+node_ip);
		if (map.get("result").equals("success")){
			//启动成功返回200
			return ResultFactory.buildSuccessResult(map.get("containerId"));
		}else {
			//failed 返回400
			return ResultFactory.buildFailResult(map.get("containerId").toString());
		}
	}

	//供后台算法调用启动容器
	@RequestMapping(value="/runContainer",method= RequestMethod.GET)
	public String dockerRun(@RequestParam("ms")  String ms_name, @RequestParam("node_ip") String node_ip,@RequestParam("container_port") Integer container_port) throws IOException {

		logger.info(ms_name+container_port+node_ip);
		Map<String,Object> res = rundockerService.runDocker(ms_name,node_ip,container_port);

		if (res.get("result").equals("success")){
			//启动成功返回200
			return "200";
		}else {
			//failed 返回400
			return "400";
		}
	}

	//后台算法kill pod
	@RequestMapping(value = "/killContainer",method = RequestMethod.GET)
	public String dockerKill(@RequestParam("containerId")String container_id,@RequestParam("nodeIp")String node_ip) throws IOException {
		String res = killDockerService.killDocker(container_id,node_ip);
		if (res.equals("success")){
			//kill成功返回200
			return "200";
		}else {
			//failed 返回400
			return "400";
		}
	}
}